<?php
// 后台用户模块
class ContactPersonAction extends CommonAction {
	protected $config=array('app_type'=>'master');
	
	function _search_filter(&$map) {
		if (!empty($_POST['keyword'])) {
			$map['emp_name|emp_no'] = array('like', "%" . $_POST['keyword'] . "%");
		}
	}

	public function _before_index() {
		//$model = M("Position");
		//$list = $model -> where('is_del=0') -> order('sort asc') -> getField('id,name');
		//$this -> assign('position_list', $list);

		//$model = M("Rank");
		//$list = $model -> where('is_del=0') -> order('sort asc') -> getField('id,name');
		//$this -> assign('rank_list', $list);

		//$model = M("Dept");
		//$list = $model -> where('is_del=0') -> order('sort asc') -> getField('id,name');
		//$this -> assign('dept_list', $list);

		//$model = M("Rank");
		//$list = $model -> where('is_del=0') -> order('sort asc') -> getField('id,name');
		//$this -> assign('rank_list', $list);
	}


	// 插入数据
	public function insert() {
		// 创建数据对象
		$model = D("ContactPerson");
		if (!$model -> create()) {
			$this -> error($model -> getError());
		} else {
			// 写入帐号数据
			$model -> __set('letter', get_letter($model -> __get('emp_name')));
			if ($result = $model -> add()) {
				$this -> assign('jumpUrl', get_return_url());
				$this -> success('用户添加成功！');
			} else {
				$this -> error('用户添加失败！');
			}
		}
	}

	function update() {
		$name = $this -> getActionName();
		$model = D($name);
		if (false === $model -> create()) {
			$this -> error($model -> getError());
		}
		// 更新数据
		$model -> __set('letter', get_letter($model -> __get('emp_name')));
		$list = $model -> save();
		if (false !== $list) {
			//成功提示
			$this -> assign('jumpUrl', get_return_url());
			$this -> success('编辑成功!');
		} else {
			//错误提示
			$this -> error('编辑失败!');
		}
	}

  function del($id=0) {
		$name = $this -> getActionName();
		$model = D($name);
    //print_r($name);
    //print_r($model);
    $model->delete($id);
    $this -> success('删除成功!');
    //print_r('成功删除一条信息!');
    //print_r($id);
  }


	function json() {
		header("Content-Type:text/html; charset=utf-8");
		$key = $_REQUEST['key'];

		$model = M("ContactPerson");
		$where['emp_name'] = array('like', "%" . $key . "%");
		$where['emp_no'] = array('like', "%" . $key . "%");
		$where['_logic'] = 'or';
		$map['_complex'] = $where;
		$list = $model -> where($map) -> field('id,emp_name as name') -> select();
		exit(json_encode($list));
	}

}
?>
